package managedbeans.administrator;

import entities.Authorisation;
import java.io.Serializable;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import langresources.ResourcesController;
import managedbeans.GenericForumBean;
import utils.dataaccess.DBInterface;
import utils.MailBean;
import utils.UserTableModel;


@ManagedBean(name="managementBean")
@SessionScoped
public class ManagementBean extends GenericForumBean implements Serializable {

    @EJB MailBean mailBean;

    private ArrayList<Authorisation> entries;
    private Authorisation selectedUser;
    private UserTableModel userModel;
    private String notificationTitle;
    private String notificationText;
    private Integer selectedUserId;
    private Integer selectedEmail;
   
    public ManagementBean() {
        entries = DBInterface.getEntries();
        userModel = new UserTableModel(entries);
    }

    public void sendNotification(){
         showMessage(ResourcesController.extractResource("MESSAGE.INFO"), ResourcesController.extractResource("NOTIFICATION_SENT_TO")+" "+selectedUser.getEmail());
         mailBean.sendMessage(selectedUser.getEmail(), notificationTitle, notificationText);
         notificationText = "";
         notificationTitle = ""; 
    }

    public String deleteUser(Authorisation user){
        DBInterface.delete(user);
        entries = DBInterface.getEntries();
        userModel = new UserTableModel(entries);
        showMessage(ResourcesController.extractResource("MESSAGE.INFO"), ResourcesController.extractResource("USER_REMOVED"));
        return "USER_DELETED";
    }
    
    public String assignModerator(Authorisation user){
        DBInterface.changeGroup(user, 2);
        entries = DBInterface.getEntries();
        userModel = new UserTableModel(entries);
        showMessage(ResourcesController.extractResource("MESSAGE.INFO"), ResourcesController.extractResource("PERMS_DELEGATED_TO")+" "+user.getUsername());
        return "MODERATOR_ASSIGNED";
    }
    
    //GETTERS-SETTERS

    public ArrayList<Authorisation> getUsers() {
        return entries;
    }

    public void setUsers(ArrayList<Authorisation> users) {
        this.entries = users;
    }

    public ArrayList<Authorisation> getEntries() {
        return entries;
    }

    public void setEntries(ArrayList<Authorisation> entries) {
        this.entries = entries;
    }

    public String getNotificationText() {
        return notificationText;
    }

    public void setNotificationText(String notificationText) {
        this.notificationText = notificationText;
    }

    public String getNotificationTitle() {
        return notificationTitle;
    }

    public void setNotificationTitle(String notificationTitle) {
        this.notificationTitle = notificationTitle;
    }

    public Integer getSelectedUserId() {
        return selectedUserId;
    }

    public void setSelectedUserId(Integer selectedUserId) {
        this.selectedUserId = selectedUserId;
    }

    public Integer getSelectedEmail() {
        return selectedEmail;
    }

    public void setSelectedEmail(Integer selectedEmail) {
        this.selectedEmail = selectedEmail;
    }

    public Authorisation getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(Authorisation selectedUser) {
        this.selectedUser = selectedUser;
    }

    public UserTableModel getUserModel() {
        entries = DBInterface.getEntries();
        return new UserTableModel(entries);
    }

    public void setUserModel(UserTableModel userModel) {
        this.userModel = userModel;
    }

}
